package cn.csxw.openchina.ui.fragment.discoverFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRefreshFragment;

import cn.csxw.openchina.constant.Constant;
import cn.csxw.openchina.dagger2.component.DaggerCircleFriendsActivityComponent;
import cn.csxw.openchina.dagger2.module.CircleFriendsActivityModules;
import cn.csxw.openchina.dagger2.presenter.CircleFriendsActivityPresenter;
import cn.csxw.openchina.entity.ResultState;
import cn.csxw.openchina.ui.adapter.CircleFriendAdapter;
import cn.csxw.openchina.utils.Global;

public class FriendFragment extends BaseRefreshFragment {

    @Inject
    CircleFriendsActivityPresenter mPresenter;
    public CircleFriendAdapter mAdapter;
    private int mIndex;
    private String mUserUid;

    @Override
    protected RecyclerView.Adapter getAdapter() {
        if(mAdapter== null){
            mAdapter = new CircleFriendAdapter();
            mIndex = 0;
        }
        return mAdapter;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        DaggerCircleFriendsActivityComponent.builder()
                .circleFriendsActivityModules(new CircleFriendsActivityModules(this))
                .build()
                .inject(this);
        //设置刷新的监听器
        setOnRefreshListener(mOnRefreshListener);
        mUserUid = Global.getUserUid(Constant.USER_UID);
        mPresenter.callNetWork(mUserUid, 0, 1, 20);
        Global.onNetworkStateListener(mPresenter,this);
    }

    //刷新相关
    OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onUpLoadMore() {
            //上拉刷新
            if(Global.isNetworkReachable(mActivity)){
                mIndex++;
                mPresenter.callNetWork(mUserUid, mIndex, 1, 20);
            }else{
                Global.showToast("你进入了没有网络的异次元");
                noMoreData();
            }
        }
    };

    //下拉刷新
    @Override
    public void doRefresh() {
        mPresenter.callNetWork(mUserUid, 0, 1, 20);
    }

}
